<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Boletin extends CI_Controller {
	
	public function __construct()
	{
		parent::__construct();
		$this->load->model('boletin_model');
		$this->load->library('session');
		$this->load->library('upload');
		$this->load->library('image_lib');
		$this->load->library('tank_auth');
		$this->load->helper('file');
		$this->load->helper('url');
		$this->load->helper('form');
		
		// prevent caching
		$this->output->set_header('Last-Modified:'.gmdate('D, d M Y H:i:s').'GMT');
		$this->output->set_header('Cache-Control: no-store, no-cache, must-revalidate');
		$this->output->set_header('Cache-Control: post-check=0, pre-check=0',false);
		$this->output->set_header('Pragma: no-cache');
		
		// if no session, redirect to login page
		if(!$this->tank_auth->is_logged_in())
		{
			redirect('admin/login', 'refresh');
		}
	}
	
	public function index()
	{
		if($this->session->flashdata('message'))
		{
			$data['message'] = $this->session->flashdata('message');
		}
		$data['row'] = $this->boletin_model->get();		
		$data['title'] = 'Administrador - Starscamp';
		$data['session'] = array('username'=>ucfirst($this->tank_auth->get_username()));
		
		$this->load->view('admin/templates/header', $data);
		$this->load->view('admin/boletin', $data);
		$this->load->view('admin/templates/footer', $data);
	}
	
	public function registro()
	{	
		if($_FILES)
		{
			$input = array();
			
			if($_FILES && $_FILES['photo']['error'] === 0)
			{				
				// initialize upload
				$this->upload->initialize(array(
					'upload_path'=>'assets/boletin/',
					'allowed_types'=>'gif|jpg|png',
					'file_name'=>'banner',
					'overwrite'=>TRUE,
					'max_size'=>4096
				));
				// A file was uploaded
				if($this->upload->do_upload('photo'))
				{
					$upload_data = $this->upload->data();
					$input['photo'] = 'assets/boletin/'.$upload_data['file_name'];	
					
					// image
					$target['width'] = 698;
					$target['height'] = 610;
					$upload_aspect_ratio = $upload_data['image_width'] / $upload_data['image_height'];
					$target_aspect_ratio = $target['width'] / $target['height'];
					if($upload_aspect_ratio < $target_aspect_ratio)
					{
						$resize['width'] = $target['width'];
						$resize['height'] = $target['width'] * 10;
					}
					else
					{
						$resize['width'] = $target['height'] * 10;
						$resize['height'] = $target['height'];
					}
					
					// resize
					$this->image_lib->initialize(array(
						'image_library'=>'gd2',
						'source_image'=>'./assets/boletin/'.$upload_data['file_name'],
						'maintain_ratio'=>TRUE,
						'width'=>$resize['width'],
						'height'=>$resize['height']
					));
					$this->image_lib->resize();
					// crop
					$this->image_lib->clear();
					$this->image_lib->initialize(array(
						'image_library'=>'gd2',
						'source_image'=>'./assets/boletin/'.$upload_data['file_name'],
						'maintain_ratio'=>FALSE,
						'width'=>$target['width'],
						'height'=>$target['height'],
						'x_axis'=>0,
						'y_axis'=>0
					));
					$this->image_lib->crop();
				}
				else
				{
					$data['upload'] = $this->upload;
				}
			}
			if($_FILES && $_FILES['html']['error'] === 0)
			{				
				// initialize upload
				$this->upload->initialize(array(
					'upload_path'=>'assets/boletin/',
					'allowed_types'=>'html',
					'file_name'=>'boletin',
					'overwrite'=>TRUE,
					'max_size'=>4096
				));
				// A file was uploaded
				if($this->upload->do_upload('html'))
				{
					$upload_data = $this->upload->data();
					$input['html'] = 'assets/boletin/'.$upload_data['file_name'];
				}
				else
				{
					$data['upload'] = $this->upload;
				}
			}
			if(isset($input['photo']) || isset($input['html']))
			{
				$this->boletin_model->update($input);
				$this->session->set_flashdata('message', 'Se ha modificado el boletín');
				
				redirect('admin/boletin', 'refresh');
			}
		}
		$data['title'] = 'Administrador - Starscamp';
		$data['session'] = array('username'=>ucfirst($this->tank_auth->get_username()));
		$data['row'] = $this->boletin_model->get();
		
		$this->load->view('admin/templates/header', $data);
		$this->load->view('admin/boletin-registro', $data);
		$this->load->view('admin/templates/footer', $data);
	}
	
	public function desvincular()
	{
		$input = array('html'=>NULL);
		$this->boletin_model->update($input);
		
		$this->session->set_flashdata('message', 'Se ha desvinculado el archivo HTML del boletín');
		redirect('admin/boletin', 'refresh');
	}
}

/* End of file boletin.php */
/* Location: ./application/controllers/admin/boletin.php */